package com.xc.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.demo.entity.Menu;
import com.xc.demo.mapper.MenuMapper;
import com.xc.demo.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Override
    public List<Menu> findMenus(String uname) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        if(!StrUtil.isBlank(uname)){
            queryWrapper.like("name",uname);

            queryWrapper.orderByAsc("id");
        }



//        查询所有数据
        List<Menu> list = list(queryWrapper);
//        找出pid为null的一级菜单
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
//        找出一级菜单的子菜单
        for(Menu menu : parentNodes){
//            找出所有pid为父级id的就是二级菜单
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
